import { controller, post,get } from "../lib/decorator";
import {checkPassword,addUser} from "../service/user"
@controller('/api/v0/user')
export class UserController{
    @get('/index')
    async index(ctx,next){
        ctx.state.title='火灾预警系统'
        await ctx.render('home/index',{session:ctx.session})
    }
    @get('/login_info')
    async login_info(ctx,next){
        ctx.state.title='火灾预警系统'
        await ctx.render('home/login')
    }
    @post('/login')
    async login(ctx,next){

        const {username,password} = ctx.request.body
        
        const result = await checkPassword(username,password)
        ctx.set('Content-Type','application/json')
        if(result.match&&result.user){
            ctx.session.currentUser = result.user
            ctx.body = {result:'登录成功'}
        }else{
            ctx.body = {result:'登录失败'}
        }
    }

    @get('/register_info')
    async register_info(ctx,next){
        ctx.state.title='火灾预警系统'
        await ctx.render('home/register')
    }
    @post('/register')
    async register(ctx,next){
        //ctx.body = ctx.request.body
        const user = await addUser(ctx.request.body)
        if(user){
            await ctx.render('home/login')
        }
    }

    @get('/:id')
    async getUserDetail(ctx,next){
        
    }
}